<?php
class AdminDoithuong extends CActiveRecord{

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }   

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'doi_thuong';
    }
    
    public function countAllRow($type_key,$key_word,$type,$status,$formDate,$toDate){
        $str = "";
        if($type_key == 1){
            $str .= " AND user_name LIKE '%$key_word%'";
        }else if($type_key == 2){
            $str .= " AND user_mobile LIKE '%$key_word%'";
        }
        if($type > 0){
            $str .= " AND `type` = $type";
        }
        if($status >= 0){
            $str .= " AND `status` = $status"; 
        }
        $sql = "SELECT count(id) as count FROM doi_thuong WHERE create_date BETWEEN '$formDate' AND '$toDate' ".$str;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
        $result = $row['count'];
        return $result; 
    }
    
    public function getAllRow($type_key,$key_word,$type,$status,$formDate,$toDate,$begin,$end){
        $row = array();
        $str = "";
        if($type_key == 1){
            $str .= " AND user_name LIKE '%$key_word%'";
        }else{
            $str .= " AND user_mobile LIKE '%$key_word%'";
        }
        if($type > 0){
            $str .= " AND `type` = $type";
        }
        if($status >= 0){
            $str .= " AND `status` = $status"; 
        }
        $sql = "SELECT * FROM doi_thuong WHERE create_date BETWEEN '$formDate' AND '$toDate' ".$str." LIMIT $begin,$end";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row; 
    }
    
    public function getItemById($doithuong_id){
        $row = array();
        $sql = "SELECT * FROM chitiet_doithuong WHERE doithuong_id = $doithuong_id";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row;    
    }
    
    public function updateStatusById($id,$status){
        $modify_date = time();
        $sql = "UPDATE doi_thuong SET status = $status, modify_date = $modify_date WHERE id = $id";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
}